//
//  YYJ_RefreshGifHeader.m
//  ANDA_Club
//
//  Created by Lonely丶晏袁杰 on 2019/2/25.
//  Copyright © 2019 Lonely`yyj. All rights reserved.
//

#import "YYJ_RefreshGifHeader.h"
@interface YYJ_RefreshGifHeader()
@property (nonatomic, strong)NSMutableArray *kImageArrays;

@end
@implementation YYJ_RefreshGifHeader
+(instancetype)YYJ_headerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock{

    return [YYJ_RefreshGifHeader headerWithRefreshingBlock:refreshingBlock];;
}
- (void)layoutSubviews{
    [super layoutSubviews];

    // 指示器 gif图的frame
    CGFloat width = CGRectGetWidth(self.bounds);
    CGFloat height = CGRectGetHeight(self.bounds);
    self.gifView.frame = CGRectMake((width - 277*0.3)/2.f, (height - 277*0.3)/2.f, 277*0.3, 277*0.3);
//    self.gifView.center = self.center;
     

//    if (self.stateHidden) {
//
//    self.gifView.contentMode = UIViewContentModeCenter;
//
//    } else {
//
//    self.gifView.contentMode = UIViewContentModeScaleAspectFit;
//
//    self.gifView.mj_w = self.mj_w * 0.5 - 90;
//
//    }
}
#pragma mark - 实现父类的方法
- (void)prepare {
    [super prepare];
    CGFloat duration = 1;
    //普通状态
    [self setImages:self.kImageArrays duration:duration forState:MJRefreshStateIdle];
    //即将刷新状态
    [self setImages:self.kImageArrays duration:duration forState:MJRefreshStateWillRefresh];
    //正在刷新状态
    [self setImages:self.kImageArrays duration:duration forState:MJRefreshStateRefreshing];
}

#pragma mark - 获取资源图片
-(NSMutableArray *)kImageArrays{
    if (!_kImageArrays) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"刷新图标-ANDAClub" ofType:@"gif"];
        _kImageArrays = [self praseGIFDataToImageArray:[NSData dataWithContentsOfFile:path]];
    }
    return _kImageArrays;
}
-(NSMutableArray *)praseGIFDataToImageArray:(NSData *)data{
    NSMutableArray *frames = [[NSMutableArray alloc] init];
    CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)data, NULL);
    CGFloat animationTime = 0.f;
    if (src) {
        size_t l = CGImageSourceGetCount(src);
        frames = [NSMutableArray arrayWithCapacity:l];
        for (size_t i = 0; i < l; i++) {
            CGImageRef img = CGImageSourceCreateImageAtIndex(src, i, NULL);
            NSDictionary *properties = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(src, i, NULL));
            NSDictionary *frameProperties = [properties objectForKey:(NSString *)kCGImagePropertyGIFDictionary];
            NSNumber *delayTime = [frameProperties objectForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
            animationTime += [delayTime floatValue];
            if (img) {
                [frames addObject:[self image:[UIImage imageWithCGImage:img] byScalingToSize:CGSizeMake(277*0.3, 207*0.3)]];
                CGImageRelease(img);
            }
        }
        CFRelease(src);
    }
    return frames;
}
- (UIImage *)image:(UIImage*)image byScalingToSize:(CGSize)targetSize {
    UIImage *sourceImage = image;
    UIImage *newImage = nil;

    UIGraphicsBeginImageContext(targetSize);

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointZero;
    thumbnailRect.size.width  = targetSize.width;
    thumbnailRect.size.height = targetSize.height;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage ;
}
- (NSArray *)getRefreshingImageArrayWithStartIndex:(NSInteger)startIndex endIndex:(NSInteger)endIndex {
    NSMutableArray * imageArray = [NSMutableArray array];
    for (NSUInteger i = startIndex; i <= endIndex; i++) {
        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"Loading_%zd.tiff",i]];
        if (image) {
            [imageArray addObject:image];
        }
    }
    return imageArray;
}
#pragma mark - 实现父类的方法
- (void)placeSubviews {
    [super placeSubviews];
    //隐藏状态显示文字
    self.stateLabel.hidden = YES;
    //隐藏更新时间文字
    self.lastUpdatedTimeLabel.hidden = YES;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
